package org.dc.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import org.dc.entity.Shift;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @user: Vladimir Chernishov
 * @date: 2/29/12
 * @time: 6:13 PM
 */
public class ShiftDaoImpl extends SQLiteOpenHelper implements ShiftDao {

    public ShiftDaoImpl(Context context) {
        super(context, "shift.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Shift (shift_id integer primary key autoincrement,\n" +
                "name text, fromTime integer, tillTime integer, color integer);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS Shift");
        onCreate(db);
    }

    @Override
    public List<Shift> getShifts(){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("Shift",new String[]{"shift_id", "name", "fromTime", "tillTime, color"}, null, null, null, null, "shift_id");
        List<Shift> ret = new ArrayList<Shift>();
        while(cursor.moveToNext()){
            Shift shift = new Shift();
            shift.setShiftId(cursor.getInt(0));
            shift.setName(cursor.getString(1));
            shift.setFrom(cursor.getInt(2));
            shift.setTill(cursor.getInt(3));
            shift.setColor(cursor.getInt(4));
            ret.add(shift);
        }
        return ret;
    }
    
    @Override
    public void save(Shift shift){
        SQLiteDatabase db = getWritableDatabase();
        if(shift.getShiftId() == 0) {
            insertShift(shift, db);
        } else {
            updateShift(shift, db);
        }
    }

    private void insertShift(Shift shift, SQLiteDatabase db) {
        ContentValues values = new ContentValues();
        values.put("name", shift.getName());
        values.put("fromTime", shift.getFrom());
        values.put("tillTime", shift.getTill());
        values.put("color", shift.getColor());
        db.insertOrThrow("Shift", null, values);
    }

    private void updateShift(Shift shift, SQLiteDatabase db) {
        ContentValues values = new ContentValues();
        values.put("name", shift.getName());
        values.put("fromTime", shift.getFrom());
        values.put("tillTime", shift.getTill());
        values.put("color", shift.getColor());
        db.update("Shift", values, "shift_id = ?", new String[]{String.valueOf(shift.getShiftId())});
    }

    @Override
    public void delete(Shift shift) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete("Shift", "shift_id = ?", new String[]{String.valueOf(shift.getShiftId())});
    }

    @Override
    public Map<Integer, Shift> getShiftsMap() {
        HashMap<Integer, Shift> ret = new HashMap<Integer, Shift>();
        for (Shift shift : this.getShifts()) {
            ret.put(shift.getShiftId(), shift);
        }
        return ret;
    }
}
